<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 会员用户管理
 */
namespace app\allwin\model;
use think\Model;

class CardUser extends Model{

    protected $pk = 'id';
    protected $table = 'ai_allwin_card_user';
    
    /**
     * 好店
     * @return void
     */
    public function store(){
        return $this->hasOne('AllwinStore','id','store_id');
    }
    
    /**
     * 储值卡
     * @return void
     */
    public function card(){
        return $this->hasOne('Card','id','card_id');
    }
    
    /**
     * 优惠券
     * @return void
     */
    public function couponUser(){
        return $this->hasOne('CouponUser','id','user_coupon_id');
    }

    //用户
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','user_id');
    }
       
    /**
     * 待删除
     * 某个用户的会员卡列表（关联查询性能较慢）
     * @param array  $condition
     * @param integer $page
     * @return void
     */
    public static function userCardList(array $condition,int $page = 10){
        return self::view('allwin_card_user', '*')
                ->view('allwin_store', 'name as store_name', 'allwin_store.id = allwin_card_user.store_id')
                ->view('allwin_card', 'name,tips', 'allwin_card.id = allwin_card_user.card_id')
                ->where($condition)->paginate($page,true);
    } 

    /**
     * 待删除
     * 读取某个用户的单个储值卡信息
     * @param array  $condition
     * @param integer $page
     * @return void
     */
    public static function userCard(array $condition){
        return self::view('allwin_card_user', '*')
                ->view('allwin_store', 'name as store_name,img,address,telphone,state_text,is_top,tags', 'allwin_store.id = allwin_card_user.store_id')
                ->view('allwin_card', 'name,discount,tips', 'allwin_card.id = allwin_card_user.card_id')
                ->where($condition)->find();
    }
}